package com.erp.warehouse.clouderp_warehouse.controller;

import com.alibaba.excel.EasyExcel;
import com.erp.warehouse.clouderp_warehouse.pojo.inbound.InboundOrder;
import com.erp.warehouse.clouderp_warehouse.pojo.inbound.InboundOrderExportDTO;
import com.erp.warehouse.clouderp_warehouse.service.inbound.InboundOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 2025/11/15 15:39
 * author: 向远洋
 */
@RestController
@RequestMapping("/download")
public class DownloadController {
    @Autowired
    InboundOrderService inboundOrderService;

    @PostMapping("/inbound")
    public void downLoadInboundList(HttpServletResponse response, @RequestBody Map<String, Object> params) throws Exception {
        // 1. 查询全部数据（确保 service 不分页！）
        List<InboundOrder> list = inboundOrderService.allInboundOrderList(params);

        // 2. 转换为导出 DTO（含中文）
        List<InboundOrderExportDTO> exportList = list.stream()
                .map(this::convertToExportDTO)
                .collect(Collectors.toList());

        // 3. 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 支持动态文件名（可从 params 取，或固定）
        String fileName = URLEncoder.encode("入库单列表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 4. 导出
        EasyExcel.write(response.getOutputStream(), InboundOrderExportDTO.class)
                .sheet("入库单")
                .doWrite(exportList); // 注意：用 DTO 类，不是 InboundOrder.class
    }


    private InboundOrderExportDTO convertToExportDTO(InboundOrder order) {
        InboundOrderExportDTO dto = new InboundOrderExportDTO();
        dto.setCode(order.getCode());
        dto.setBusinessTypeName(getTypeName(order.getType()));
        dto.setTotalQuantity(order.getTotalQuantity());
        dto.setTotalMoney(order.getTotalMoney());
        dto.setProviderName(order.getProviderName());
        dto.setStaffName(order.getStaffName());
        dto.setAuditorName(order.getAuditorName());
        dto.setStatusName(getStatusName(order.getStatus()));
        dto.setPrintNumber(order.getPrintNumber());
        return dto;
    }

    private String getTypeName(Byte type) {
        if (type == null) return "";
        switch (type) {
            case 1: return "采购入库";
            case 2: return "销售退货";
            case 3: return "调拨入库";
            default: return "未知(" + type + ")";
        }
    }

    private String getStatusName(Integer status) {
        if (status == null) return "";
        switch (status) {
            case 0: return "已创建";
            case 1: return "已审核";
            case 2: return "已作废";
            default: return "未知(" + status + ")";
        }
    }
}
